<?php


class Api_changepassword_mdl extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function outputdata($data)
	{
		if ( !empty( $data['EMAIL'] ) && !empty( $data['PASSWORD'] ) && !empty( $data['NEWPASSWORD'] )) {
			
			//验证用户是否存在
			$user = array();
			$user = $this->_checkUser( $data['EMAIL'] );

			if (!empty($user) ) {
			//用户存在	
				//是否激活
				if ( $user['active'] ) {
					
					if ( Common::hash_Validate($data['PASSWORD'], $user['txpassword'] ) ) {
						
						//验证密码
						$password = Common::do_hash( $data['NEWPASSWORD'] );
	
						if ( $this->_changePassword($user['uid'],$password) ) {
							
							$outputdata['TAG'] = $data['TAG'];
							$outputdata['STATUS'] = 0;
							$outputdata['ERRORMESSAGE'] = 'SUCCESSFULL';
							//直接登录
							$this->load->library('auth');
							
							$user['txpassword'] = $password;
							$user['active'] = 1;
							$token = $this->auth->process_login($user);
							$userinfo['TOKEN'] = $token;
							$userinfo['EMAIL'] = $data['EMAIL'];
							$userinfo['USERID'] = (int)$user['uid'];
							$userinfo['LASTLOGIN'] = date('Y-m-d H:i:s',$user['lastlogin']);
							$userinfo['GOLD'] = (int)$user['txgoldnum'];
							$userinfo['USERNAME'] = $user['txusername'];
							
							$outputdata['USERINFO'] = $userinfo;
							
						} else {
							
							//原密码错误
							$outputdata['TAG'] = $data['TAG'];
							$outputdata['STATUS'] = 1;
							$outputdata['ERRORMESSAGE'] = 'SYATEM ERROR';
							$outputdata['USERINFO'] = null;
						}
						
					} else {
						
						//原密码错误
						$outputdata['TAG'] = $data['TAG'];
						$outputdata['STATUS'] = 1;
						$outputdata['ERRORMESSAGE'] = 'PASSWORD ERROR';
						$outputdata['USERINFO'] = null;
						
					}
					
				} else {
					//未激活
					$outputdata['TAG'] = $data['TAG'];
					$outputdata['STATUS'] = 1;
					$outputdata['ERRORMESSAGE'] = 'NO ACTIVE';
					$outputdata['USERINFO'] = null;
					
				}
				
			} else {
				
				//用户不存在
				$outputdata['TAG'] = $data['TAG'];
				$outputdata['STATUS'] = 1;
				$outputdata['ERRORMESSAGE'] = 'NO USER';
				$outputdata['USERINFO'] = null;
			}
			
		} else {
			
			//信息不完整
			$outputdata['TAG'] = $data['TAG'];
			$outputdata['STATUS'] = 1;
			$outputdata['ERRORMESSAGE'] = 'INFO ERROR';
			$outputdata['USERINFO'] = null;
			
		}
		
		header('Content-Type:text/html; charset=utf-8', false);
		echo json_encode($outputdata);
		
	}
	
	/**
	 * 
	 * 验证用户是否存在
	 * @param String $email
	 */
	private function _checkUser( $email )
	{
		$user = array();
		$user = $this->db->select( 'uid,txusername,txemail,active,lastlogin,txpassword,txgoldnum' )
						->where( 'txemail', $email )
						->get( 'cj_users' )
						->row_array();
		return $user;
	}
	
	/**
	 * 
	 * 修改密码
	 * @param int $uid
	 * @param String $password
	 */
	private function _changePassword($uid,$password)
	{
		$data = array( 'txpassword' => $password );
		
		$this->db->where( 'uid', $uid );	
		return $this->db->update( 'cj_users', $data );
		
	}
	
}